package com.rajaev.dtu.tractor.interfaces;

import com.github.pagehelper.PageInfo;

import com.rajaev.dtu.tractor.app.service.FaultService;
import com.rajaev.dtu.tractor.domain.entity.UserInfo;
import com.rajaev.dtu.tractor.domain.vo.BatteryFaultStatisticVo;
import com.rajaev.dtu.tractor.domain.vo.FaultDetailVo;
import com.rajaev.dtu.tractor.domain.vo.FaultItemVo;
import com.rajaev.dtu.tractor.domain.vo.FaultVo;
import com.rajaev.dtu.tractor.infrastructure.helper.UserSecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Created with IDEA
 * @author: tianjie
 * @Date:2019/4/4
 * @Time:11:35
 */
@RestController
public class FaultController {

    @Autowired
    FaultService faultService;

    @Autowired
    UserSecurityUtil userSecurityUtil;


    @GetMapping("/fault/pageQueryFaults")
    public PageInfo<FaultVo> pageQueryFaults(@RequestParam(defaultValue = "10") Integer pageSize,
                                             @RequestParam(defaultValue = "1")Integer pageNum,
                                             String vin ,HttpServletRequest request) {
        return faultService.selectFaults(request,pageSize, pageNum, vin);
    }

    /**
     * 查询当前用户下所有设备的故障统计信息
     * @return
     */
    @GetMapping("/fault/queryBatteryFaultStatisticVo")
    public BatteryFaultStatisticVo queryBatteryFaultStatisticVo(
            @RequestParam(defaultValue = "2020-01-01")
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date searchBegin,
            @RequestParam(defaultValue = "2099-01-01")
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date searchEnd, HttpServletRequest request){
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();
        Integer userId = userInfo.getId();

        return faultService.queryBatteryFaultStatisticVo(userId,searchBegin,searchEnd);
    }

    @GetMapping("/fault/selectFaultBatteryNumber")
    public Integer selectFaultBatteryNumber( HttpServletRequest request){
        UserInfo userInfo = userSecurityUtil.getLoginUser(request).getUserInfo();

        return faultService.selectFaultNumber(userInfo.getId());
    }

    @GetMapping("/fault/pageQueryFaultDetails")
    public PageInfo<FaultDetailVo> queryFaultDetail(@RequestParam(defaultValue = "10") Integer pageSize,
                                                    @RequestParam(defaultValue = "1")Integer pageNum,
                                                    @RequestParam(value = "vin",defaultValue = "") String vinOrPlateNo,
                                                    @RequestParam(required = false,value = "faultLevel")Integer faultLevel,
                                                    @RequestParam(required = false,defaultValue = "2020-01-01",value = "searchBegin")
                                                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date searchBegin ,
                                                    @RequestParam(required = false,defaultValue = "2999-01-01",value = "searchEnd")
                                                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date searchEnd){
        return faultService.selectFaultDetail(pageSize, pageNum, vinOrPlateNo,faultLevel,searchBegin,searchEnd);
    }

    @GetMapping("/fault/pageQueryFaultWarns")
    public PageInfo queryFaultWarn(@RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "1")Integer pageNum,HttpServletRequest request){
        return faultService.selectBatteryWarnInfos(request,pageSize, pageNum);
    }

    /**
     * 查询故障字典列表
     * @param pageSize
     * @param pageNum
     * @param faultCode
     * @param faultDesc
     * @return
     */
    @GetMapping("/fault/pageQueryFaultList")
    public PageInfo<FaultItemVo> pageQueryFaultItems(@RequestParam(defaultValue = "10") Integer pageSize,
                                                     @RequestParam(defaultValue = "1")Integer pageNum,
                                                     String faultCode, String faultDesc){
        return faultService.pageQueryFaultItems(pageSize, pageNum, faultDesc,faultCode);
    }

    @PostMapping("/fault/updateFaultItem")
    public void updateFaultItem(FaultItemVo faultItemVo){
        faultService.updateFaultItem(faultItemVo);
    }



}
